package start.spring.basic.flowable.controller.process;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.annotation.RequiresPermissions;

import start.spring.basic.flowable.controller.BaseController;
import start.spring.basic.flowable.entity.WfDeployForm;
import start.spring.basic.flowable.service.flowable.IWfCopyService;
import start.spring.basic.flowable.service.process.IWfProcessService;
import start.spring.basic.flowable.vo.flowable.ProcessQuery;
import start.spring.basic.flowable.vo.flowable.WfCopyBo;
import start.spring.basic.flowable.vo.flowable.WfCopyVo;
import start.spring.basic.flowable.vo.flowable.WfDefinitionVo;
import start.spring.basic.flowable.vo.flowable.WfDetailVo;
import start.spring.basic.flowable.vo.flowable.WfTaskVo;
import start.spring.basic.page.PageQuery;
import start.spring.basic.response.ResponseData;
import start.spring.basic.response.ResponsePage;

/**
 * 工作流流程管理
 *
 * @author KonBAI
 * @createTime 2022/3/24 18:54
 */
@RestController
@RequestMapping("/process")
public class WfProcessController extends BaseController {

	@Autowired
    private IWfProcessService processService;
    
	@Autowired
	private IWfCopyService copyService;

    /**
     * 查询可发起流程列表
     *
     * @param pageQuery 分页参数
     */
    @PostMapping(value = "/list")
    @RequiresPermissions("workflow:process:startList")
    public ResponsePage<WfDefinitionVo> startProcessList(PageQuery pageQuery,@RequestBody ProcessQuery processQuery) {
        return super.returnResponse(processService.selectPageStartProcessList(pageQuery, processQuery));
    }

    /**
     * 我拥有的流程
     */
    @RequiresPermissions("workflow:process:ownList")
    @PostMapping(value = "/ownList")
    public ResponsePage<WfTaskVo> ownProcessList(@RequestBody ProcessQuery processQuery, PageQuery pageQuery) {
        return super.returnResponse(processService.selectPageOwnProcessList(processQuery, pageQuery));
    }
    
    /**
     * 获取待签列表
     *
     * @param processQuery 流程业务对象
     * @param pageQuery 分页参数
     */
    @RequiresPermissions("workflow:process:claimList")
    @PostMapping(value = "/claimList")
    public ResponsePage<WfTaskVo> claimProcessList(@RequestBody ProcessQuery processQuery, PageQuery pageQuery) {
        return super.returnResponse(processService.selectPageClaimProcessList(processQuery, pageQuery));
    }

    /**
     * 获取待办列表
     */
    @RequiresPermissions("workflow:process:todoList")
    @GetMapping(value = "/todoList")
    public ResponsePage<WfTaskVo> todoProcessList(ProcessQuery processQuery, PageQuery pageQuery) {
        return super.returnResponse(processService.selectPageTodoProcessList(processQuery, pageQuery));
    }

    /**
     * 获取已办列表
     *
     * @param pageQuery 分页参数
     */
    @RequiresPermissions("workflow:process:finishedList")
    @PostMapping(value = "/finishedList")
    public ResponsePage<WfTaskVo> finishedProcessList(ProcessQuery processQuery, PageQuery pageQuery) {
        return super.returnResponse(processService.selectPageFinishedProcessList(processQuery, pageQuery));
    }

    /**
     * 获取抄送列表
     *
     * @param copyBo 流程抄送对象
     * @param pageQuery 分页参数
     */
    @RequiresPermissions("workflow:process:copyList")
    @PostMapping(value = "/copyList")
    public ResponsePage<WfCopyVo> copyProcessList(WfCopyBo copyBo, PageQuery pageQuery) {
        return super.returnResponse(copyService.selectPageList(copyBo, pageQuery));
    }

    
    /**
     * 删除抄送列表
     *
     * @param ids 待删除记录id
     * 
     */
    @RequiresPermissions("workflow:process:copyList")
    @PostMapping(value = "/deleteCopyList")
    public ResponseData<String> deleteCopyList(@RequestBody List<String> ids) {
        return this.copyService.deleteCopyList(ids);
    }
    
    /**
     * 查询流程部署关联表单信息
     *
     * @param definitionId 流程定义id
     * @param deployId 流程部署id
     */
    @GetMapping("/getProcessForm")
    @RequiresPermissions("workflow:process:startList")
    public R<?> getForm(@RequestParam(value = "definitionId") String definitionId,
                        @RequestParam(value = "deployId") String deployId,
                        @RequestParam(value = "procInsId", required = false) String procInsId) {
        return R.ok(processService.selectFormContent(definitionId, deployId, procInsId));
    }
    
    /**
     * 查询流程部署关联外部表单信息
     *
     * @param definitionId 流程定义id
     * @param deployId 流程部署id
     */
    @GetMapping("/getDeployExternalForm")
    @RequiresPermissions("workflow:process:startList")
    public ResponseData<WfDeployForm> getDeployExternalForm(@RequestParam(value = "definitionId") String definitionId,
                        @RequestParam(value = "deployId") String deployId) {
        return ResponseData.successData(processService.selectDeployExternalForm(definitionId, deployId));
    }

    /**
     * 根据流程定义id启动流程实例
     *
     * @param processDefId 流程定义id
     * @param variables 变量集合,json对象
     */
    @RequiresPermissions("workflow:process:startList")
    @PostMapping("/start/{processDefId}")
    public R<String> start(@PathVariable(value = "processDefId") String processDefId, @RequestBody Map<String, Object> variables) {
        processService.startProcessByDefId(processDefId, variables);
        return R.ok("流程启动成功");
    }

    /**
     * 读取xml文件
     * @param processDefId 流程定义ID
     */
    @GetMapping("/bpmnXml/{processDefId}")
    public ResponseData<String> getBpmnXml(@PathVariable(value = "processDefId") String processDefId) {
        return ResponseData.successData(processService.queryBpmnXmlById(processDefId));
    }

    /**
     * 查询流程详情信息
     *
     * @param procInsId 流程实例ID
     * @param taskId 任务ID
     */
    @GetMapping("/detail")
    public ResponseData<WfDetailVo> detail(String procInsId, String taskId, String businessKey) {
        return processService.queryProcessDetail(procInsId, taskId, businessKey);
    }
    
    /**
     * 查询流程详情信息
     *
     * @param procInsId 流程实例ID
     * @param taskId 任务ID
     */
    @PostMapping("/delete")
    public ResponseData<String> delete(@RequestBody List<String> instanceIds) {
        processService.deleteProcessByIds(instanceIds);
        return ResponseData.successMsg("已删除！");
    }
}
